<?php

class Zy_install extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		header("Content-type:text/html;charset=utf-8");
	}

	function index()
	{
		$this->load->view('Zy_view_install');
	}

	function change_phpini()
	{
		$myfile = "../../php/php.ini";
		$myfilebak = "../../php/php_bak.ini";
		$tofile = "../../php/php_new.ini";

		$file_handle = fopen($myfile, "r");
		
		if (!$file_handle)
		{
			return;
		}

		$to_handle = fopen($tofile, "w");
		
		if (!$to_handle)
		{
			fclose($file_handle);
			return;
		}

		$newcfgs = array(
			'memory_limit = ' => '500M',
			'upload_max_filesize = ' => '8M',
		);
			
		while (!feof($file_handle)) {
			$line = fgets($file_handle);

			foreach ($newcfgs as $k => &$v)
			{
				if (!strncmp($k, $line, strlen($k)))
				{
			   	$line = $k.$v."\r\n";
			   	break;
				}				
			}
		  
			fwrite($to_handle, $line);
		}
		
		fclose($file_handle);
		fclose($to_handle);
		
		rename($myfile, $myfilebak);
		rename($tofile, $myfile);
	}

	function install()
	{
		$dbtable = $this->load->get_var('dbtable');

		$this->load->database();
		
		$querystring = "CREATE TABLE IF NOT EXISTS `$dbtable` ( ";
		$querystring .= "`index` int(11) NOT NULL AUTO_INCREMENT, ";
		$querystring .= "`name` text NOT NULL, ";
		$querystring .= "`sex` varchar(1) NOT NULL, ";
		$querystring .= "`age` int(11) NOT NULL DEFAULT '0', ";
		$querystring .= "`date` date NOT NULL, ";
		$querystring .= "`diag` text NOT NULL, ";
		$querystring .= "`prescribe` text NOT NULL, ";
		$querystring .= "`usemethod` text NOT NULL, ";
		$querystring .= "`comment` text NOT NULL, ";
		$querystring .= "UNIQUE KEY `index` (`index`) ";
		$querystring .= ") ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;";

		if (!$this->db->query($querystring))
		{
			echo "初始化失败!";
			return;
		}

		if (!$this->db->field_exists('phonenum', $dbtable))
		{
			$querystring = "alter table `$dbtable` add phonenum varchar(32);";
	
			if (!$this->db->query($querystring))
			{
				echo "初始化失败!";
				return;
			}
		}

		if (!$this->db->field_exists('addr', $dbtable))
		{
			$querystring = "alter table `$dbtable` add addr text;";
	
			if (!$this->db->query($querystring))
			{
				echo "初始化失败!";
				return;
			}
		}

		if (!$this->db->field_exists('files', $dbtable))
		{
			$querystring = "alter table `$dbtable` add files text;";
	
			if (!$this->db->query($querystring))
			{
				echo "初始化失败!";
				return;
			}
		}	

		if (!$this->db->field_exists('attackdate', $dbtable))
		{
			$querystring = "alter table `$dbtable` add attackdate date;";
	
			if (!$this->db->query($querystring))
			{
				echo "初始化失败!";
				return;
			}
		}

		$prescribetable = $this->load->get_var('prescribetable');
		
		$querystring = "CREATE TABLE IF NOT EXISTS `$prescribetable` ( ";
		$querystring .= "`name` varchar(128) NOT NULL, ";
		$querystring .= "`value` text NOT NULL, ";
		$querystring .= "UNIQUE KEY `name` (`name`) ";
		$querystring .= ") ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;";

		if (!$this->db->query($querystring))
		{
			echo "初始化失败!";
			return;
		}

		if (!$this->db->field_exists('usemethod', $prescribetable))
		{
			$querystring = "alter table `$prescribetable` add usemethod text;";
	
			if (!$this->db->query($querystring))
			{
				echo "初始化失败!";
				return;
			}
		}
				
		$this->change_phpini();

		$zydatadir = "../zydata";
		
		if (!file_exists($zydatadir))
		{
			if (!mkdir($zydatadir))
			{
				echo "创建目录".$zydatadir."失败！";
				return;
			}
		}

		$srcfile = "./images/background.jpg";
		$dstfile = "../zydata/background.jpg";

		if (!file_exists($dstfile))
		{
			copy($srcfile, $dstfile);
		}

		$srcfile = "./zy.cfg";
		$dstfile = "../zydata/zy.cfg";

		if (!file_exists($dstfile))
		{
			copy($srcfile, $dstfile);
		}

		$this->load->view('Zy_view_installsuccess');
	}
}

?>